package com.zxw.flow.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zxw.common.core.base.enums.EnumBeanNameEnum;
import com.zxw.common.web.util.BeanUtils;
import com.zxw.common.web.util.JwtUtils;
import com.zxw.flow.domain.entity.FlowNodesModel;
import com.zxw.flow.domain.entity.FlowNodesModel;
import com.zxw.flow.domain.entity.FlowType;
import com.zxw.flow.domain.vo.FlowNodesModelPageVO;
import com.zxw.flow.domain.vo.FlowNodesModelVO;
import com.zxw.flow.mapper.FlowNodesModelMapper;
import com.zxw.flow.service.FlowNodesModelService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Xuewei.Zhang
 * @since 2022-07-04
 */
@Service
@RequiredArgsConstructor
public class FlowNodesModelServiceImpl extends ServiceImpl<FlowNodesModelMapper, FlowNodesModel> implements FlowNodesModelService {

    private final FlowNodesModelMapper flowNodesModelMapper;

    @Override
    public IPage<FlowNodesModel> getPage(FlowNodesModelPageVO pageVO) {
        Page<FlowNodesModel> flowNodesModelDTOPage = new Page<>(pageVO.getPageNum(), pageVO.getPageSize());
        LambdaUpdateWrapper wrapper = new LambdaUpdateWrapper<FlowNodesModel>()
                .eq(FlowNodesModel::getCompanyId, pageVO.getCompanyId())
                .eq(null != pageVO.getNodeName(), FlowNodesModel::getNodeName, pageVO.getNodeName())
                .eq(null != pageVO.getShape(), FlowNodesModel::getShape, pageVO.getShape());
        Page<FlowNodesModel> pageData = flowNodesModelMapper.selectPage(flowNodesModelDTOPage, wrapper);
        return pageData;
    }

    @Override
    public FlowNodesModelVO add(FlowNodesModelVO flowNodesModelVO) {
        flowNodesModelMapper.insert(flowNodesModelVO);
        return flowNodesModelVO;
    }

    @Override
    public FlowNodesModel getFlowNodesModelById(Long id) {
        Long companyId = JwtUtils.getCompanyId();
        FlowNodesModel flowNodesModel = flowNodesModelMapper.selectOne(new LambdaQueryWrapper<FlowNodesModel>()
                .eq(FlowNodesModel::getId, id)
                .eq(FlowNodesModel::getCompanyId, companyId)
        );
        return flowNodesModel;
    }

    @Override
    public void updateBase(FlowNodesModelVO flowNodesModelVO) {
        flowNodesModelMapper.update(flowNodesModelVO, new LambdaUpdateWrapper<FlowNodesModel>()
                .eq(FlowNodesModel::getId, flowNodesModelVO.getId())
                .eq(FlowNodesModel::getCompanyId, flowNodesModelVO.getCompanyId())
        );
    }

    @Override
    public void delete(String flowNodesModelIds) {
        Long companyId = JwtUtils.getCompanyId();
        flowNodesModelMapper.delete(new LambdaUpdateWrapper<FlowNodesModel>()
                .eq(FlowNodesModel::getCompanyId, companyId)
                .in(FlowNodesModel::getId, flowNodesModelIds)
        );
    }
}
